package org.bitterorange.flansmodslain.interaction;

import lk.vexview.api.VexViewAPI;
import lk.vexview.gui.components.VexImage;
import lk.vexview.hud.VexImageShow;
import net.minecraft.server.v1_7_R4.Entity;
import net.minecraft.server.v1_7_R4.EntityPlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer;
import org.bukkit.entity.Player;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;

import static com.flansmod.common.guns.type.GrenadeType.grenades;
import static org.bitterorange.flansmodslain.FlansModAPI.getPlayerVehicle;
import static org.bitterorange.flansmodslain.FlansModAPI.spawnGrenade;

public class DebugCommand implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {
//        if (args.length != 2) {
//            FlansModSlain.debug();
//            return false;
//        }
        try {
            if (args[0].equalsIgnoreCase("field")) {
                for (Field field : Class.forName(args[1]).getDeclaredFields()) {
                    commandSender.sendMessage(field.toGenericString());
                }
            } else if (args[0].equalsIgnoreCase("method")) {
                for (Method method : Class.forName(args[1]).getDeclaredMethods()) {
                    commandSender.sendMessage(method.toGenericString());
                }
            } else if (args[0].equalsIgnoreCase("open")) {
                if (!(commandSender instanceof Player)) {
                    commandSender.sendMessage("只有玩家才能执行此命令!");
                    return true;
                }
                VexImage vexImage = new VexImage(args[1], 256, 256, 512, 512);
                VexImageShow vexImageShow = new VexImageShow(args[1], vexImage, 1000);
                VexViewAPI.sendHUD((Player) commandSender, vexImageShow);
            }
            else if(args[0].equalsIgnoreCase("ride")){
                Player p = (Player) commandSender;
                Object entity = p.getVehicle();
                if(entity == null) return false;
                System.out.println(p.getVehicle().getEntityId());
                System.out.println(p.getVehicle().getUniqueId());
                System.out.println(p.getVehicle().getType().name());
                System.out.println(p.getVehicle().getType().getEntityClass());
                System.out.println(p.getVehicle().getType());
                Class<?> clazz = entity.getClass();

                for (Field declaredField : clazz.getDeclaredFields()) {
                    System.out.println("FIELD = " + declaredField.toGenericString());
                }

                for (Method declaredMethod : clazz.getDeclaredMethods()) {
                    System.out.println("METHOD = " + declaredMethod.toGenericString());
                }

                EntityPlayer entityPlayer = ((CraftPlayer) p).getHandle();
                Entity vehicle = entityPlayer.vehicle;

                System.out.println(vehicle.getClass());
                System.out.println(vehicle);

                Object info = vehicle.getClass().getMethod("getAcInfo").invoke(vehicle);
                HashMap<?, ?> map = (HashMap<?, ?>) info.getClass().getField("displayNameLang").get(info);
                System.out.println(map.get("en_US"));

                System.out.println("1111111111");
                System.out.println(getPlayerVehicle(p));
            } else if (args[0].equalsIgnoreCase("bullet")) {
                grenades.forEach(t -> {
                    try {
                        System.out.println(t.getClass().getField("shortName").get(t));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
                spawnGrenade(((Player)commandSender).getWorld(), args[1], (Player) commandSender);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
